
//
//  StringSizeTool.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/18.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "StringSizeTool.h"

@implementation StringSizeTool
//宽度一定,计算高度
+ (CGFloat)contentSizeOfStringHeight:(NSString *)content maxWidth:(CGFloat)width fontSize:(CGFloat)fontSize{
    if (content.length == 0) {
        return CGSizeZero.height;
    }
    //返回包含字符串的边框
    //1:指定限制大小:限制宽度,不限高度
    //2:NSStringDrawingUsesLineFragmentOrigin->从左上角开始计算
    //3:NSStringDrawingUsesFontLeading->包含行间距
    //4:attributes:字典:指定使用的字体
    //5:context:上下文,可为nil
    CGRect rect = [content boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil];
    return rect.size.height;
}

//高度一定,计算出宽度
+ (CGFloat)contentSizeOfStringWidth:(NSString *)content maxHeight:(CGFloat)height fontSize:(CGFloat)fontSize{
    if (content.length == 0) {
        return CGSizeZero.width;
    }
    //返回包含字符串的边框
    //1:指定限制大小,限制宽度,高度不限
    //2:NSStringDrawingUsesLineFragmentOrigin->从左上角开始计算
    //3:NSStringDrawingUsesFontLeading->包含行间距
    //4:attributes:字典:指定使用的字体
    //5:context:上下文,可为nil
    CGRect rect = [content boundingRectWithSize:CGSizeMake(height, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil];
    return rect.size.width;
}

@end
